<template>
  <div>
    <div class="header">
      <div class="head">
        <div class="same">
          <position />
          <input type="text" placeholder="请搜索商品" />
          <div class="tab">
            <div class="nav">
              <span
                class="nav_btn"
                :class="isShow == true ? 'avtive' : ''"
                @click="(isShow = true), Shop()"
                >首页</span
              >
              <span
                class="nav_btn"
                :class="item.tabsName == name && !isShow ? 'avtive' : ''"
                v-for="(item, i) in tabs"
                :key="item.id"
                @click="navBtn(item.tabsName, i, item), (isShow = false)"
              >
                {{ item.tabsName }}
              </span>
            </div>
          </div>
        </div>
      </div>

      <CommBtn class="Commbtn" />
      <div class="erBtn" v-if="!isShow">
        <div class="same">
          <div class="btn">
            <div class="nav">
              <span
                @click="NavBtn(i, item2.contentName)"
                :class="i === indexNav ? 'erBtnacitve' : ''"
                v-for="(item2, i) in tabs[index].content"
                :key="item2.id"
                >{{ item2.contentName }}</span
              >
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="main" v-if="!isShow">
      <div class="same">
        <div class="shop">
          <div class="box" v-for="item3 in list" :key="item3.id">
            <router-link :to="'/detail/' + item3.id">
              <img :src="`http://192.168.10.23:8888/` + item3.shopImg" alt="" />
              <dl>
                <dt>
                  {{ item3.shopName }}
                </dt>
                <dd>￥{{ item3.shopPrice }}</dd>
              </dl>
            </router-link>
          </div>
        </div>
      </div>
    </div>
    <div class="home" v-if="isShow">
      <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
        <div class="same">
          <div class="swipersame">
            <div class="swiper mySwiper">
              <div class="swiper-wrapper">
                <div class="swiper-slide" v-for="item in banner" :key="item.id">
                  <img :src="item.img" alt="" />
                </div>
              </div>
            </div>
          </div>

          <div class="recommend">
            <div class="til">
              <h2>为您推荐</h2>
            </div>
            <van-empty
              v-show="recommend.length == 0"
              image="network"
              description="网络错误"
            />
            <div class="row">
              <div
                v-show="recommend.length !== 0"
                class="box"
                v-for="item in recommend"
                :key="item.id"
              >
                <router-link :to="'/detail/' + item.id">
                  <!-- <img :src="item.img" alt="" /> -->
                  <img
                    :src="`http://192.168.10.23:8888/` + item.shopImg"
                    alt=""
                  />
                  <!-- <img
                  src="https://tse1-mm.cn.bing.net/th/id/R-C.052a1dd51a463657fc7d57a38c4df816?rik=YOTOKfjjUjswAQ&riu=http%3a%2f%2flaisheji-web.oss-cn-shenzhen.aliyuncs.com%2fdefault%2f20191128%2f661897117a2c6221c73b10773a31a5dbeCHQHAHjxs.jpg%3fx-oss-process%3dstyle%2fwmlogo&ehk=luYAkpsA12kk4a0qwF5j%2b64MJC7HZKnt63oTQW%2faRZU%3d&risl=&pid=ImgRaw&r=0"
                  alt=""
                /> -->
                  <dl>
                    <dt>{{ item.shopName }}</dt>
                    <dd>￥{{ item.shopPrice }}</dd>
                  </dl>
                </router-link>
              </div>
            </div>
          </div>
        </div>
      </van-pull-refresh>
    </div>
  </div>
</template>

<script>
import Swiper from "swiper";
import { Toast } from "vant";

import CommBtn from "../../components/CommBtn.vue";
import "../../../node_modules/swiper/css/swiper.css";
import { Shop, shopcall, shopAll } from "@/api";
import Position from "../../components/Position.vue";
export default {
  components: { Position, CommBtn },

  computed: {},
  data() {
    return {
      recommend: [],
      list: [],
      banner: [
        {
          id: 1,
          img: "https://img.zcool.cn/community/0161fa5d9c507ca8012060beab8251.jpg@1280w_1l_2o_100sh.jpg",
        },
        {
          id: 2,
          img: "https://img.zcool.cn/community/0163a15bae2992a8012099c87d9f4a.png@2o.png",
        },
        {
          id: 3,
          img: "https://cbu01.alicdn.com/img/ibank/2020/551/398/22265893155_1262084193.jpg",
        },
        {
          id: 4,
          img: "https://cbu01.alicdn.com/img/ibank/2019/679/188/10746881976_1262084193.jpg",
        },
        {
          id: 5,
          img: "https://img.zcool.cn/community/01b6295e078c83a801216518404d2f.jpg@1280w_1l_2o_100sh.jpg",
        },
      ],
      isShow: true,
      tabs: [],
      name: "宠物主粮",
      ername: "国产粮",
      index: 0,
      indexNav: 0,
      isLoading: false,
    };
  },
  created() {
    this.Shop();
    this.shopTuijian();
  },
  mounted() {},
  methods: {
    onRefresh() {
      setTimeout(() => {
        Toast("刷新成功");
        this.shopTuijian();
        this.isLoading = false;
      }, 1000);
    },
    shopTuijian() {
      shopAll().then((res) => {
        // console.log(res.data.data);
        this.recommend = res.data.data;
      });
    },

    NavBtn(index, name) {
      // console.log(name);
      this.indexNav = index;
      shopcall(name).then((res) => {
        // console.log(res);
        this.list = res.data.data;
      });
    },
    navBtn(name, index, item) {
      this.name = name;
      this.index = index;
      this.indexNav = 0;
      this.NavBtn(0, item.content[0].contentName);
    },
    Shop() {
      Shop().then((res) => {
        // console.log(res.data.data, "okok");
        this.tabs = res.data.data;
      });
      setTimeout(() => {
        new Swiper(".mySwiper", {
          autoplay: true,

          loop: true,
        });
      }, 1000);
    },
  },
};
</script>

<style scoped lang="scss">
.Commbtn {
  position: fixed;
  right: 15px;
  bottom: 20vh;
  z-index: 10;
}
.home {
  width: 100%;
  height: auto;
  margin-top: 2vh;
  overflow: hidden;

  .swipersame {
    width: 100%;
    overflow: hidden;
    border-radius: 10px;
    .swiper-slide {
      img {
        width: 100%;
        height: 22vh;
      }
    }
  }
  .recommend {
    width: 100%;
    height: auto;
    margin-top: 3vh;
    background: rgba(255, 255, 255, 0.308);
    border-radius: 10px;
    .til {
      height: 5vh;
      // background: red;
      line-height: 5vh;
      h2 {
        font-weight: normal;
        font-size: 20px;
        margin-left: 2vw;
      }
    }
    .row {
      width: 100%;
      height: auto;
      display: flex;
      justify-content: space-between;
      flex-wrap: wrap;
      .box {
        width: 48%;
        height: 36vh;
        background: rgb(255, 255, 255);
        border-radius: 10px;
        margin-bottom: 2vh;
        overflow: hidden;
        a {
          font-size: 16px;
          color: #333;
          img {
            width: 100%;
            height: 20vh;
          }
          dl {
            width: 95%;
            font-size: 14px;
            margin: 0 auto;
            dt {
              color: #333;
              height: 6vh;
              overflow: hidden;
              margin: 2vh 0;
            }
            dd {
              color: rgb(211, 92, 23);
              position: relative;
              del {
                position: absolute;

                font-size: 12px;
                color: #aaa;
              }
            }
          }
        }
      }
    }
  }
}
.header {
  width: 100%;
  background: transparent;
  z-index: 5;
  top: 0;
  position: sticky;

  .head {
    width: 100%;
    height: 24vh;

    background: #fff;
    input {
      width: 95%;
      height: 5vh;
      border-radius: 20px;
      text-indent: 2em;
      outline: none;
      border: 1px solid orange;
      margin: 10px;
    }
    .tab {
      width: 100%;
      height: 6vh;
      overflow-y: hidden;

      .nav {
        width: 200%;
        height: 6vh;
        display: flex;
        align-items: center;
        justify-content: space-between;
        .nav_btn {
          width: 50vw;
          height: 4vh;
          line-height: 4vh;
          text-align: center;
          border-radius: 8px;
        }
      }
    }
  }
}
.avtive {
  padding: 1px;
  background: rgb(255, 136, 0);
  color: #fff;
}
.erBtnacitve {
  color: rgb(255, 136, 0);
}
.main {
  width: 100%;
  height: 100vh;
  padding-top: 2vh;
  background: rgba(250, 249, 249, 0.733);
  .shop {
    width: 100%;
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    .box {
      width: 45%;
      height: 30vh;
      background: rgb(255, 255, 255);
      padding: 5px;
      margin-top: 1vh;
      a {
        color: #333;
        img {
          width: 100%;
          height: 18vh;
        }
        dt {
          height: 6vh;
          font-size: 16px;
          overflow: hidden;
        }
        dd {
          margin-top: 1vh;
          color: rgb(238, 137, 23);
        }
      }
    }
  }
}
.erBtn {
  width: 100%;
  height: 6vh;
  background: rgb(243, 243, 243);

  .btn {
    width: 100%;
    overflow-y: hidden;
    .nav {
      width: 125%;
      height: 6vh;
      display: flex;
      align-items: center;
      span {
        width: 25vw;
        height: 4vh;
        text-align: center;
        line-height: 4vh;
      }
    }
  }
}
</style>
